﻿using MediatR;
using Traceability.APP.Application.Command.Traceability;
using Traceability.APP.DTOS;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrerture.Interfaces;

namespace Traceability.APP.Application.CommandHandle.Traceability
{
    /// <summary>
    /// 溯源基本信息显示
    /// </summary>
    public class GetShowTraceabilityCommandHandle:IRequestHandler<GetShowTraceabilityCommand, APIResult<APIPing<TraceabilityDto>>>
    {
        private readonly IBaseRepository<Traceability_Management> TraceabilityDal;
        private readonly IBaseRepository<Shop_Model> ShopDal;

        public GetShowTraceabilityCommandHandle(IBaseRepository<Traceability_Management> traceabilityDal, IBaseRepository<Shop_Model> shopDal)
        {
            TraceabilityDal = traceabilityDal;
            ShopDal = shopDal;
        }

        public Task<APIResult<APIPing<TraceabilityDto>>> Handle(GetShowTraceabilityCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPing<TraceabilityDto>> result = new APIResult<APIPing<TraceabilityDto>>();
            result.Code = StateCode.Ok;
            result.Message = StateCode.Ok.ToString();
            //var list = TraceabilityDal.GetAll().Where(x => x.IsDeleted == false);
            //var shoname = ShopDal.GetAll();

            var list = from a in TraceabilityDal.GetAll()
                       join b in ShopDal.GetAll() on a.SID equals b.SID
                       where !a.IsDeleted
                       select new TraceabilityDto
                       {
                           ID = a.ID,
                           IsDeleted = a.IsDeleted,
                           Batch = a.Batch,
                           ShelfLife = a.ShelfLife,
                           Production = a.Production,
                           Enterprise = a.Enterprise,
                           Shop_Name = b.Shop_Name,
                           Message = a.Message,
                           CreateName = a.CreateName,
                           CreateTime = a.CreateTime,
                           UpdateName = a.UpdateName,
                           UpdateTime = a.UpdateTime,
                           SID = a.SID,
                       };



            //商品名称查询
            if (!string.IsNullOrEmpty(request.Shop_Name))
            {
                list = list.Where(x => x.Shop_Name.Contains(request.Shop_Name));
            }

            //生产批次查询
            if (!string.IsNullOrEmpty(request.Batch))
            {
                list = list.Where(x => x.Batch.Contains(request.Batch));
            }

            //保质期查询
            if (!string.IsNullOrEmpty(request.ShelfLife))
            {
                list = list.Where(x => x.ShelfLife.Contains(request.ShelfLife));
            }

            //生产许可证号
            if (!string.IsNullOrEmpty(request.Production))
            {
                list = list.Where(x => x.Production.Contains(request.Production));
            }

            //生产企业
            if (!string.IsNullOrEmpty(request.Enterprise))
            {
                list = list.Where(x => x.Enterprise.Contains(request.Enterprise));
            }

            //创建人
            if (!string.IsNullOrEmpty(request.CreateName))
            {
                list = list.Where(x => x.CreateName.Contains(request.CreateName));
            }

            //修改人
            if (!string.IsNullOrEmpty(request.UpdateName))
            {
                list = list.Where(x => x.UpdateName.Contains(request.UpdateName));
            }

            var totaCount = list.Count();
            var pageSizer = (int)Math.Ceiling(totaCount * 1.0 / request.Pagesizer);
            var pageData = list.OrderByDescending(x => x.ID).Skip((request.Pageidenx - 1) * request.Pagesizer).Take(request.Pagesizer);

            APIPing<TraceabilityDto> ping = new APIPing<TraceabilityDto>()
            {
                TotaCount = totaCount,
                PageCount = pageSizer,
                PageData = pageData.ToList()
            };

            result.Data = ping;
            return Task.FromResult(result);



        }


    }
}
